package com.xgst.land.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.android.material.button.MaterialButton
import com.gym.baselib.base.BaseActivity
import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.xgst.land.R
import com.xgst.land.bean.IceboxItemListBean
import com.xgst.land.bean.SampleDetailBean
import com.xgst.land.bean.SampleTypeBean
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import java.lang.Exception

//样本类型
class TypeLandDialog(context: Context):BaseDialog(context,600f) {
    override fun getLayoutRes() = R.layout.dialog_type_layout

    private var itemDialog = ItemListDialog(context)
    private var mId = -1

    val imgBG = view.findViewById<ImageView>(R.id.imgBG)
    val llProgress = view.findViewById<LinearLayout>(R.id.llProgress)
    val mbNegative = view.findViewById<MaterialButton>(R.id.mbNegative)
    val mbPositive = view.findViewById<MaterialButton>(R.id.mbPositive)
    val tvProjectName = view.findViewById<TextView>(R.id.tvProjectName)

    fun show(onSave:(String,String)->Unit) {
        super.show()
        imgBG.setOnClickListener {
            llProgress.visibility = View.VISIBLE
            projects({
                val list = mutableListOf<IceboxItemListBean>()
                it.forEach { bean->
                    list.add(IceboxItemListBean(bean.id,bean.name))
                }
                llProgress.visibility = View.GONE
                itemDialog.show(list,{name,id ->
                    tvProjectName.text = name
                    mId = id
                })
            })
        }
        mbNegative.setOnClickListener {
            dismiss()
        }
        mbPositive.setOnClickListener {
            if(mId == -1)return@setOnClickListener
            onSave("$mId",tvProjectName.text.toString())
            dismiss()
        }
    }

    //项目
    private fun projects(onResult:(MutableList<SampleTypeBean.DataBean>)->Unit){
        EasyHttp.post(ActivityUtils.getTopActivity() as AppCompatActivity)
            .api("other/app_sampleCategoryDict/findAllSampleCategoryDictList.do")
            .request(object : OnHttpListener<SampleTypeBean>{
                override fun onSucceed(result: SampleTypeBean) {
                    onResult(result.data)
                }

                override fun onFail(e: Exception) {
                    ToastUtils.showShort(e.message)
                    llProgress.visibility = View.GONE
                }
            })
    }
}